package com.heima.behavior.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.behavior.mapper.ApLikesBehaviorMapper;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApLikesBehaviorService;
import com.heima.common.dtos.Result;
import com.heima.common.exception.AppHttpCodeEnum;
import com.heima.common.exception.LeadNewsException;
import com.heima.model.behavior.dtos.LikesBehaviorDto;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApLikesBehavior;
import com.heima.model.behavior.pojos.ApUnlikesBehavior;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.common.ThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 * APP点赞行为表 服务实现类
 * </p>
 *
 * @author heima
 * @since 2021-08-16
 */
@Service
@Transactional
public class ApLikesBehaviorServiceImpl extends ServiceImpl<ApLikesBehaviorMapper, ApLikesBehavior> implements ApLikesBehaviorService {
    @Autowired
    private ApBehaviorEntryService apBehaviorEntryService;
    @Override
    public Result likeBehavior(LikesBehaviorDto dto) {
        ApUser apUser = (ApUser) ThreadLocalUtils.get();
        Integer userId = null;
        if (userId != null){
            userId = apUser.getId();
        }
        Integer equipmentId = dto.getEquipmentId();
        ApBehaviorEntry behaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(userId, equipmentId);

        if (behaviorEntry != null){
            throw new LeadNewsException(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        QueryWrapper<ApLikesBehavior> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("entry_id",behaviorEntry.getEntryId());
        queryWrapper.eq("articler_id",dto.getArticleId());
        ApLikesBehavior likesBehaviorb = getOne(queryWrapper);
        if (likesBehaviorb == null && dto.getOperation().equals(0)){
            ApLikesBehavior likesBehavior = new ApLikesBehavior();
            likesBehavior.setCreatedTime(new Date());
            likesBehavior.setEntryId(behaviorEntry.getId());
            likesBehavior.setArticleId(dto.getArticleId());
            likesBehavior.setType(0);
            likesBehavior.setOperation(dto.getOperation());
            save(likesBehavior);
        }else {
            likesBehaviorb.setOperation(dto.getOperation());
            updateById(likesBehaviorb);
        }
        return Result.ok();

    }
//查询点赞
    @Override
    public ApLikesBehavior findLike(Integer entryId, Long articleId) {
        QueryWrapper<ApLikesBehavior> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("entry_id",entryId);
        queryWrapper.eq("article_id",articleId);
        return getOne(queryWrapper);
    }
}
